/* fafamtm.p - FIXED ASSETS MAINTENANCE - Retirement Book Display        */
/* Copyright 1986 QAD Inc. All rights reserved.                      */
/* $Id:: fafamtm.p 59128 2015-08-21 10:49:10Z nsc      $: */
/* $Revision: 1.9.1.14 $							 */
/* REVISION: 9.1      LAST MODIFIED: 10/26/99	BY: *N021* Pat Pigatti	    */
/* REVISION: 9.1      LAST MODIFIED: 03/24/00	BY: *N08T* Annasaheb Rahane */
/* REVISION: 9.1      LAST MODIFIED: 05/17/00	BY: *M0LJ* Vihang Talwalkar */
/* REVISION: 9.1      LAST MODIFIED: 07/28/00	BY: *N0BX* Arul Victoria    */
/* REVISION: 9.1      LAST MODIFIED: 08/14/00	BY: *N0L0* Jacolyn Neder    */
/* REVISION: 9.1      LAST MODIFIED: 10/18/00	BY: *M0VB* Shilpa Athalye   */
/* Revision: 1.9.1.11  BY: Manisha Sawant	 DATE: 10/29/01 ECO: *M1P6* */
/* Revision: 1.9.1.13  BY: Paul Donnelly (SB)	 DATE: 06/26/03 ECO: *Q00C* */
/* REVISION: 17360     LAST MODIFIED: 05/04/12    BY: ddo                     */
/* $Revision: 1.9.1.14 $  BY: Vandna Rohira        DATE: 12/17/04 ECO: *P2Z5* */
/*-Revision end---------------------------------------------------------------*/

/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

{us/bbi/mfdeclre.i}
/* EXTERNAL LABEL INCLUDE */
{us/bbi/gplabel.i}

{us/gp/gprunpdf.i "mcpl" "p"}  

/* ********** BEGIN TRANSLATABLE STRINGS DEFINITION ********* */
&SCOPED-DEFINE fafamtm_p_1 "Basis Amount"
/* MaxLen: 16 Comment: */

&SCOPED-DEFINE fafamtm_p_2 "Net Amount"
/* MaxLen: 16 Comment: */

&SCOPED-DEFINE fafamtm_p_3 "Gain/Loss"
/* MaxLen: 16 Comment: */

&SCOPED-DEFINE fafamtm_p_4 "Accum Dep"
/* MaxLen: 16 Comment: */

&SCOPED-DEFINE fafamtm_p_5 "End"
/* MaxLen: 6 Comment: Button label - End*/

/* ********** END TRANSLATABLE STRINGS DEFINITION ********* */

define input parameter p-status as character format "x(25)" no-undo.
define parameter buffer fa_mstr for fa_mstr.

/* --------------------     DEFINE QUERY  ------------------- */
define query q_fab_det for
   fab_det
   scrolling.

/* -----------------  STANDARD VARIABLES  ---------------- */
define variable perform-status  as character format "x(25)"
   initial "first" no-undo.
define variable p-skip-update   like mfc_logical no-undo.
define variable l-rowid         as rowid     no-undo.
define variable l-delete-it     like mfc_logical no-undo.
define variable l-del-rowid     as rowid     no-undo.
define variable l-top-rowid     as rowid     no-undo.
define variable lines           as integer
   initial 10 no-undo.
define variable l-found         like mfc_logical no-undo.
define variable pos             as integer   no-undo.
define variable l-first         like mfc_logical no-undo.
define variable l-error         like mfc_logical no-undo.
define variable l-title         as character    no-undo.
define variable l_yrper         like fabd_yrper no-undo.
define variable l_err           as character    no-undo.
define variable l_amt_fmt       as character    no-undo.
define variable l_bmt_fmt       as character    no-undo.
define variable mc-error-number like msg_nbr no-undo.
define variable rndmthd         like rnd_rnd_mthd no-undo.
define variable l-primary-curr   as character no-undo.
define variable l-primary-bcrate like fab_bc_exr_rate no-undo.
define variable l-primary-book   as character no-undo.
define variable l-fa-dispamt     like fa_dispamt no-undo.
/* ------------------  BUTTON VARIABLES     ----------------- */
define button b-end label {&fafamtm_p_5}.

/* -------------  STANDARD WIDGET VARIABLES  ------------- */
define variable l-focus         as widget-handle no-undo.
define variable w-frame         as widget-handle no-undo.

/* ------------------  SCREEN VARIABLES     ----------------- */
define variable l-basis-amt     like fab_amt initial 0 no-undo.
define variable l-net-amt       like fab_amt initial 0 no-undo.
define variable l-gain-loss     like fab_amt initial 0 no-undo.
define variable l-acc-amt       like fabd_accamt initial 0 no-undo.
define variable l-basis-amt-fmt as character     format "x(30)" no-undo.
define variable l-acc-amt-fmt   as character     format "x(30)" no-undo.
define variable l-net-amt-fmt   as character     format "x(30)" no-undo.
define variable l-gain-loss-fmt as character     format "x(30)" no-undo.
define variable l-curr          as character     format "x(3)"  no-undo.
define variable l_rnd_mthd      like rnd_rnd_mthd               no-undo.
/* ------------------  FRAME DEFINITION	 ----------------- */
/* ADDED SIDE-LABELS PHRASE TO FRAME STATEMENT		    */

define frame f-button
   b-end at 1
   with no-box overlay three-d side-labels width 10.

/* CLEAR FRAME REGISTRATION TO TRANSLATE THE FRAME EVERYTIME */
clearFrameRegistration(frame f-button:handle).

/* SET EXTERNAL LABELS */
setFrameLabels(frame f-button:handle).

assign
   l-focus            = b-end:handle in frame f-button
   b-end:width        = 8
   b-end:private-data = "end".

define frame f-1
   skip(.4)
   fab_fabk_id
   l-basis-amt column-label {&fafamtm_p_1}
   l-acc-amt   column-label {&fafamtm_p_4} 
   l-net-amt   column-label {&fafamtm_p_2} 
   l-gain-loss column-label {&fafamtm_p_3}
   l-curr
   with three-d overlay 4 down scroll 1 width 81.

/* CLEAR FRAME REGISTRATION TO TRANSLATE THE FRAME EVERYTIME */
clearFrameRegistration(frame f-1:handle).

/* SET EXTERNAL LABELS */
setFrameLabels(frame f-1:handle).

run ip-framesetup.

w-frame = frame f-1:handle.
{us/bbi/gprun.i ""fafmtut.p""
   "(w-frame)"}

lines = 04.

if p-status = "add"
   then 
do:
   frame f-1:visible = true.
   return.
end. /* IF p-status = "add" */

/* ---------------  PRE PROCESSING INCLUDE  -------------- */
/* VARIABLES FOR FA PROCEDURES */
{us/gp/gprunpdf.i "fapl" "p"}

/* SET CURRENCY DEPENDENT ROUNDING FORMATS */
for first gl_ctrl  where gl_ctrl.gl_domain =
   global_domain no-lock:
end. /* FOR FIRST gl_ctrl */

/* find primary book currency */
for first fabk_mstr where fabk_domain = global_domain
                      and fabk_primary = yes no-lock,
    first cu_mstr of fabk_mstr no-lock:
    assign l-primary-curr = cu_curr
           l-primary-book = fabk_id.
    for first fab_det where fab_det.fab_domain = global_domain
                        and fab_fa_id = fa_mstr.fa_id
                        and fab_fabk_id = l-primary-book
                        and fab_resrv = 0 no-lock:
        assign l-primary-bcrate = fab_det.fab_bc_exr_rate * fab_det.fab_bc_exr_ratescale.
    end.
end. 

assign
   l-basis-amt-fmt = l-basis-amt:format
   l-acc-amt-fmt   = l-acc-amt:format
   l-net-amt-fmt   = l-net-amt:format
   l-gain-loss-fmt = l-gain-loss:format
   l-fa-dispamt    = fa_dispamt.

/* -------------  END PRE PROCESSING INCLUDE  ------------ */

open query q_fab_det for each fab_det
   where fab_det.fab_domain = global_domain and  fab_fa_id = fa_mstr.fa_id
   and     fab_resrv = 0
   use-index fab_fa_id no-lock.

get first q_fab_det no-lock.
        

l_amt_fmt = l-net-amt:format in frame f-1.
l_bmt_fmt = l-basis-amt:format in frame f-1.

main-loop:
do while perform-status <> "end" on error undo:

   if perform-status = "" and
      p-status       = "first-one"
   then
      return.

   if p-status = "end"
   then
      perform-status = "end".

   run ip-query
      (input-output perform-status,
      input-output l-rowid).

   if p-status = "first-one" and
      not available fab_det
   then do:
      frame f-1:visible = true.
      return.
   end. /* IF p-status = "first-one" */

   /* ----------------------  DISPLAY  ---------------------- */
   run ip-predisplay.

   /* -----------------------  END  ----------------------- */
   if perform-status = "end"
   then do:
      frame f-1:hidden = yes.
      hide frame f-1 no-pause.
      delete procedure this-procedure no-error.
      leave.
   end. /* IF perform-status = "end" */

   /* ------------------  SELECTION BUTTONS  ---------------- */
   if p-status         <> "first-one" and
      p-status         <> "end" and
      perform-status <> "first"
   then
      run ip-button (input-output perform-status).

end. /* DO WHILE perform-status <> "end" */

PROCEDURE ip-predisplay:
   if (perform-status = "" or
      perform-status = "update") and
      available fab_det
   then
   display-loop:
   do:
      if perform-status = ""
      then do:
         clear frame f-1 all no-pause.
         l-top-rowid = rowid(fab_det).
      end. /* IF perform-status = "" */

      read-loop:
      do pos = 1 to lines:
         if perform-status = ""
         then do:
            if session:display-type = "gui"
            then
               assign
                  fab_fabk_id:bgcolor = 8
                  fab_fabk_id:fgcolor = 0.
            else
               color display normal fab_fabk_id
                  with frame f-1.
         end. /* IF perform-status = "" */

         run ip-displayedits.
         if perform-status = "update"
         then
            leave display-loop.
         if pos < lines
         then
            down with frame f-1.
         get next q_fab_det no-lock.
         if not available fab_det
         then
            leave.
      end. /* DO pos = 1 TO lines */

      run ip-postdisplay.
   end. /* DISPLAYLOOP */
END PROCEDURE. /* ip-predisplay */

PROCEDURE ip-displayedits:
   if available fab_det
   then do:
      for first fabk_mstr
         where fabk_mstr.fabk_domain = global_domain 
         and   fabk_id = fab_det.fab_fabk_id no-lock,
         first cu_mstr of fabk_mstr 
         where cu_mstr.oid_cu_mstr = fabk_mstr.oid_cu_mstr
         no-lock:
         assign 
            l-curr = cu_mstr.cu_curr.
      end.
      

      /* DISPLAY-EDITS */
      {us/gp/gprunp.i "fapl" "p" "fa-get-cost"
     "(input  fab_fa_id,
       input  fab_fabk_id,
       output l-basis-amt)"}

      /* GET YEAR/PERIOD BASED ON LAST POSTED DEPRECIATION */
      if fa_mstr.fa_disp_dt = ?
      then do:

         for last fabd_det
            no-lock
            where fabd_det.fabd_domain = global_domain and  fabd_fa_id   =
            fab_fa_id
            and   fabd_fabk_id = fab_fabk_id
            and   fabd_post    = yes
            use-index fabd_fa_id:
         end. /* FOR LAST fabd_det */

         if available fabd_det
         then
            l_yrper = fabd_yrper.
      end. /* IF fa_mstr.fa_disp_dt = ? */

      /* GET YEAR/PERIOD BASED ON RETIREMENT PERIOD  */
      else 
      do:
         for first fabk_mstr
            where fabk_mstr.fabk_domain = global_domain and  fabk_id =
            fab_fabk_id
            no-lock:
            {us/gp/gprunp.i "fapl" "p" "fa-get-per"
              "(input  fa_disp_dt,
            input  fabk_calendar,
            output l_yrper,
            output l_err)"}
         end. /* FOR FIRST fabk_mstr */

      end. /* ELSE DO */

      {us/gp/gprunp.i "fapl" "p" "fa-get-accdep"
        "(input     fab_fa_id,
          input     fab_fabk_id,
          input     l_yrper,
          output l-acc-amt)"}
      /* fa_dispamt is based on primary book currency for other books, */
      /* it needs to convert to the book currency                      */
      if fa_mstr.fa_disp_dt <> ? then
      do:
           if l-curr = l-primary-curr then assign l-fa-dispamt = fa_dispamt.
           else assign l-fa-dispamt = (fa_dispamt * l-primary-bcrate) / (fab_det.fab_bc_exr_rate * fab_det.fab_bc_exr_ratescale).
      end.
     
      /* fa_dispamt is based on primary book currency for other books, */
      /* it needs to convert to the book currency                      */
      if fa_mstr.fa_disp_dt <> ? then
      do:
           if l-curr = l-primary-curr then assign l-fa-dispamt = fa_dispamt.
           else assign l-fa-dispamt = (fa_dispamt * l-primary-bcrate) / (fab_det.fab_bc_exr_rate * fab_det.fab_bc_exr_ratescale).
      end.
     
      assign
         l-net-amt   = l-basis-amt - l-acc-amt
         l-gain-loss = - (if fa_mstr.fa_disp_dt <> ?
              then
                 l-basis-amt - (l-acc-amt + l-fa-dispamt)
              else 0).
      /* DISPLAY-EDITS */
      if l-curr = ? or l-curr = '' then
      do:
           assign l-curr = base_curr.
      end.
      {us/gp/gprunp.i "mcpl" "p" "mc-get-rnd-mthd"
        "(input l-curr,
          output rndmthd,
          output mc-error-number)"}
      if mc-error-number <> 0 then 
      do:
         {us/bbi/pxmsg.i &MSGNUM=mc-error-number &ERRORLEVEL=3}
      end.
      
      l_amt_fmt = l-net-amt:format in frame f-1.
      {us/bbi/gprun.i ""gpcurfmt.p""
        "(input-output l_amt_fmt, input rndmthd)"}

      {us/gp/gprunp.i "mcpl" "p" "mc-get-rnd-mthd"
        "(input base_curr,
          output rndmthd,
          output mc-error-number)"}
      if mc-error-number <> 0 then 
      do:
         {us/bbi/pxmsg.i &MSGNUM=mc-error-number &ERRORLEVEL=3}
      end.

      {us/bbi/gprun.i ""gpcurfmt.p""
        "(input-output l_bmt_fmt, input rndmthd)"}

      assign
         l-net-amt:format in frame f-1   = l_amt_fmt
         l-gain-loss:format in frame f-1 = l_amt_fmt
         l-acc-amt:format in frame f-1   = l_amt_fmt
         l-basis-amt:format in frame f-1 = l_amt_fmt.

      run ip-display.
   end. /* IF AVAILABLE fab_det */
END PROCEDURE. /* ip-displayedits */

PROCEDURE ip-display:
   display
      fab_det.fab_fabk_id
      l-basis-amt
      l-net-amt
      l-gain-loss
      l-acc-amt
      l-curr
      with frame f-1.
END PROCEDURE. /* ip-display */

PROCEDURE ip-postdisplay:
   do pos = 1 to lines:
      if frame-line(f-1) <= 1
      then
         leave.
      up 1 with frame f-1.
   end. /* DO pos = 1 TO lines */

   if perform-status = ""
   then do:
      reposition q_fab_det to rowid l-top-rowid no-error.
      get next q_fab_det no-lock.
      l-rowid = l-top-rowid.
      if p-status <> "first-one"
      then
         color display message fab_fabk_id
            with frame f-1.
   end. /* IF perform-status = "" */
END PROCEDURE. /* ip-postdisplay */

PROCEDURE ip-query:
   define input-output parameter perform-status as character no-undo.
   define input-output parameter l-rowid as rowid no-undo.

   if perform-status = "first"
   then do:
      if l-rowid <> ?
      then do:
         reposition q_fab_det to rowid l-rowid no-error.
         get next q_fab_det no-lock.
      end. /* IF l-rowid <> ? */

      if not available fab_det
      then
         get first q_fab_det no-lock.
      if available fab_det
      then
         assign
            perform-status = ""
            l-rowid        = rowid(fab_det).
      else 
      do:
         assign
            perform-status    = ""
            l-rowid           = ?
            frame f-1:visible = true.
         return.
      end. /* ELSE DO */
   end. /* IF perform-status = "first" */

   if perform-status = "open"
   then do:
      open query q_fab_det for each fab_det
         where fab_det.fab_domain = global_domain and fab_fa_id =
         fa_mstr.fa_id
         and   fab_resrv = 0
         use-index fab_fa_id no-lock.

      reposition q_fab_det to rowid l-rowid no-error.
      get next q_fab_det no-lock.
      if available fab_det
      then
         assign
            perform-status = ""
            l-rowid        = rowid(fab_det).
      else 
      do:
         get first q_fab_det no-lock.
         if not available fab_det
         then do:
            assign
               perform-status    = ""
               frame f-1:visible = true.
            return.
         end. /* IF NOT AVAILABLE fab_det */
         else
            assign
               perform-status = ""
               l-rowid        = rowid(fab_det).
      end. /* ELSE DO*/
   end. /* IF perform-status = "open" */

   if perform-status = "next"
   then do:
      get next q_fab_det no-lock.
      if available fab_det
      then do:
         hide message no-pause.
         assign
            l-rowid        = rowid(fab_det)
            perform-status = "next".
         if session:display-type = "gui"
         then
            assign
               fab_fabk_id:bgcolor = 8
               fab_fabk_id:fgcolor = 0.
         else
            color display normal fab_fabk_id
               with frame f-1.
         /* MAKES SCROLLING WORK - DON'T REMOVE */
         pause 0.
         down 1 with frame f-1.
         run ip-displayedits.
         color display message fab_fabk_id
            with frame f-1.
      end. /* IF AVAILABLE fab_det */

      else 
      do:
         perform-status = "next".
         /* END OF FILE */
         {us/bbi/pxmsg.i &MSGNUM=20 &ERRORLEVEL=2}
         get last q_fab_det no-lock.
      end. /* ELSE DO */

   end. /* IF perform-status = "next" */

   if perform-status = "prev"
   then do:
      get prev q_fab_det no-lock.
      if not available fab_det
      then do:
         /* BEGINNING OF FILE */
         {us/bbi/pxmsg.i &MSGNUM=21 &ERRORLEVEL=2}
         get first q_fab_det no-lock.
         return.
      end. /* IF NOT AVAILABLE fab_det */

      hide message no-pause.
      l-rowid = rowid(fab_det).
      if session:display-type = "gui"
      then
         assign
            fab_fabk_id:bgcolor = 8
            fab_fabk_id:fgcolor = 0.
      else
         color display normal fab_fabk_id
            with frame f-1.
      /* MAKES SCROLLING WORK - DON'T REMOVE */
      pause 0.
      up 1 with frame f-1.
      run ip-displayedits.
      color display message fab_fabk_id
         with frame f-1.
   end. /* IF perform-status = "prev" */

   if perform-status = "update" or
      perform-status = "delete"
   then do:
      get current q_fab_det no-lock.
      if not available fab_det
      then do:
         hide message no-pause.
         /* RECORD NOT FOUND */
         {us/bbi/pxmsg.i &MSGNUM=1310 &ERRORLEVEL=3}
         perform-status = "".
      end. /* IF NOT AVAILABLE fab_det */
   end. /* IF perform-status = "update" */
END PROCEDURE. /* ip-query */

PROCEDURE ip-framesetup:
   if session:display-type = "gui"
   then
      assign
         current-window:bgcolor = 8
         frame f-1:row          = 9.5.
   else
      frame f-1:row = 9.

   assign
      frame f-1:box           = true
      frame f-1:centered      = true
      frame f-1:title         = (getFrameTitle("DEPRECIATION_BOOKS",26))
      frame f-button:centered = true
      frame f-button:row      = 20.

END PROCEDURE. /* ip-framesetup */

PROCEDURE ip-button:
   define input-output parameter perform-status as character
      format "x(25)" no-undo.

   if not batchrun
   then do:

      enable all with frame f-button.
      ststatus = stline[1].
      status input ststatus.
      on choose of b-end
         do:
            assign
               perform-status = self:private-data
               l-focus        = self:handle.
            hide frame f-1 no-pause.
            hide frame f-button no-pause.
         end. /* ON CHOOSE OF b-end */

      on cursor-up, f9 anywhere
         do:
            assign
               perform-status = "prev"
               l-focus        = focus:handle.
            apply "go" to frame f-button.
         end. /* ON CURSOR-UP, F9 ANYWHERE */

      on cursor-down, f10 anywhere
         do:
            assign
               perform-status = "next"
               l-focus        = focus:handle.
            apply "go" to frame f-button.
         end. /* ON CURSOR-DOWN, F10 ANYWHERE */

      on cursor-right anywhere
         do:
            l-focus = self:handle.
            apply "tab" to l-focus.
         end. /* ON CURSOR-RIGHT ANYWHERE */

      on cursor-left anywhere
         do:
            l-focus = self:handle.
            if session:display-type = "gui"
            then
               apply "shift-tab" to l-focus.
            else
               apply "ctrl-u" to l-focus.
         end. /* ON CURSOR-LEFT ANYWHERE */

      on esc anywhere
         do:
            if session:display-type = "gui"
            then
               apply "choose" to b-end in frame f-button.
         end. /* ON ESC ANYWHERE */

      on end-error anywhere
         apply "choose" to b-end in frame f-button.
      on window-close of current-window
         apply "choose" to b-end in frame f-button.

      on go anywhere
         do:
            if (lastkey = keycode("cursor-down") or
               lastkey  = keycode("cursor-up") or
               lastkey  = keycode("f9") or
               lastkey  = keycode("f10"))
            then
               return.
            else
               l-focus = focus:handle.
            apply "choose" to l-focus.
         end. /* ON GO ANYWHERE */

      wait-for
         go of frame f-button or
         window-close of current-window or
         choose of
         b-end
         focus
         l-focus.

   end. /* IF NOT BATCHRUN */
   else
      set perform-status.

   hide message no-pause.
END PROCEDURE. /* ip-button */
